<?php
/*
	插件概览
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'get';
if($_RQ['op']=='get'){
	$merch=pdo_get('jy_merch_user',array('id'=>$_SESSION['onemerchid'],'uniacid'=>$_SESSION['uniacid']));
	$return['plugin_icon']=$merch['logo'];
	$item=array('type'=>'0','groupid'=>'0');
	$return['item']=$item;
	$htmls=array(
		'type'=>'edit',
		'post'=>'set',
		'desc'=>'商品导出',
		'edit'=>array(
			array('type'=>'tip','title'=>'只有导出分类和商品，才是完整的！'),
			array('type'=>'radio','title'=>'导出类型','name'=>'type','options'=>'types'),
			array('type'=>'select','title'=>'导出分类？','name'=>'groupid','options'=>'groups','show'=>array('type'=>1)),
		)
	);
	$groupspram=array('merchid'=>$_SESSION['onemerchid'],'uniacid'=>$_SESSION['uniacid'],'mid'=>0,'parentid'=>0);
	if($merchid){
		$groupspram['mid']=$_SESSION['uid'];
	}
	$return['groups']=pdo_getall('jy_weishop_goods_group',$groupspram,array('id','title'),'id');
	$return['groups'][0]=array('id'=>0,'title'=>'全部分类');
	$return['htmls']=$htmls;
	$return['types']=array(
		'0'=>'导出分类',
		'1'=>'导出商品',
		'2'=>'导出门店',
	);
	if($merchid){
		unset($return['types'][2]);
	}
	exi($return);
}
if($_RQ['op']=='set'){
	$post=get_postdata();	
	$_SESSION['e2cpost']=$post;
	if($post['type']==1){
		$url=download_url('jy_weishopniu/goods_chu/down/groupid='.$post['groupid']);
	}elseif($post['type']==2){
		$url=download_url('jy_weishopniu/goods_chu/downstore/');
	}else{
		$url=download_url('jy_weishopniu/goods_chu/downgroup/');
	}
	exi('点确定开始下载','','',$url);
	
}
if($_RQ['op']=='downgroup'){
	if($_SESSION['onemerchid']){
		$mid=$_SESSION['uid'];
	}
	$this->model('chu')->down_group($mid);
}
if($_RQ['op']=='downstore'){
	$this->model('chu')->down_store();
}
if($_RQ['op']=='down'){
	$filename=random(15);
	@mkdir(ROOT_AT.$filename);
	$groupid=intval($_RQ['groupid']);
	file_put_contents(ROOT_AT.$filename.'/readme.txt','[温馨提示]：商品文件，请勿多次重复导入，否则会出现数据重复！');	
	$merchid=$_SESSION['onemerchid']?:0;
	$this->model('chu')->down_goods($filename,$groupid,$merchid);
	$params=array('uniacid'=>$_SESSION['uniacid'],'merchid'=>$merchid);
	if($groupid){
		$params['pcate']=$groupid;
	}
	$goodsids=pdo_getall('jy_weishop_goods',$params,'id','id');
	if(count($goodsids)>0){
		$goodsids=array_keys($goodsids);
		//$this->model('chu')->down_dispatch($filename);
		$this->model('chu')->down_option($filename,$goodsids);
		$this->model('chu')->down_spec($filename,$goodsids);
		$this->model('chu')->down_spec_item($filename,$goodsids);
	}
	//规格
	cfc('tools')->down_zip(ROOT_AT.$filename,$filename,true);
	exit;
}
